package com.example.javachatroomextend.controller;

import com.example.javachatroomextend.model.Message;
import com.example.javachatroomextend.model.Result;
import com.example.javachatroomextend.model.User;
import com.example.javachatroomextend.service.NewFriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/newFriend")
public class NewFriendController {
    @Autowired
    private NewFriendService newFriendService;

    @GetMapping("/getNewFriend")
    public Result<List<Message>> getNewFriend(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("user");
        List<Message> ret = newFriendService.getNewFriend(user.getUserId());
        return Result.success(ret);
    }

    @PostMapping("/delete")
    public Result<Boolean> delete(Integer fromId,Integer toId) {
        Integer ret = newFriendService.delete(fromId,toId);
        if (ret != null) return Result.success(true);
        return Result.fail(500,false,"内部出现错误，请联系管理员");
    }
}
